﻿using System;
using System.Collections.Generic;
using System.Threading;
using Core.BOL;
using Core.DAL;
using Core.Extentions;
using System.Web.Security;
using Elmah;
using System.Configuration;
using System.Net;
using System.IO;
using Core.ScheduleTask;

namespace Core.Utils
{
    public enum TaskManagerAction
    {
        ZipAlbum,
        ZipMyALbum
    }

    public static class TaskManager
    {
        #region Members
        private static string appPath;
        private static Queue<KeyValuePair<TaskManagerAction, Object>> tasksQueue;
        #endregion

        #region Methodes
        public static void Init(string applicationPath)
        {
            tasksQueue = new Queue<KeyValuePair<TaskManagerAction, Object>>();
            appPath = applicationPath;

            Thread mainThread = new Thread(new ThreadStart(ProcessQueue));
            mainThread.IsBackground = true;
            mainThread.Start();

            Thread scheduleTaskThread = new Thread(new ThreadStart(ProcessScheduleTask));
            scheduleTaskThread.IsBackground = true;
            scheduleTaskThread.Start();
        }

        public static void AddTask(KeyValuePair<TaskManagerAction, Object> taskParameters)
        {
            lock (tasksQueue)
            {
                tasksQueue.Enqueue(taskParameters);
            }
        }

        private static void ProcessScheduleTask()
        {
            while (true)
            {
                try
                {
                    ClearDatas.ClearOldZipFiles(appPath);
                    ClearDatas.ClearStatMail();
                }
                catch (Exception ex)
                {
                    ErrorLog.GetDefault(null).Log(new Error(ex));
                }

                Thread.Sleep(86400000); //One day
            }
        }

        private static void ProcessQueue()
        {
            while (true)
            {
                KeyValuePair<TaskManagerAction, Object>? taskParameters = null;

                lock (tasksQueue)
                {
                    if (tasksQueue.Count > 0)
                    {
                        taskParameters = tasksQueue.Dequeue();
                    }
                }

                if (taskParameters != null && ((KeyValuePair<TaskManagerAction, Object>)taskParameters).Value != null)
                {
                    try
                    {
                        if (taskParameters.Value.Key == TaskManagerAction.ZipAlbum)
                        {
                            AlbumFilesGenerator.CreateZipFile((string[])taskParameters.Value.Value, appPath);
                        }
                        else if (taskParameters.Value.Key == TaskManagerAction.ZipMyALbum)
                        {
                            AlbumFilesGenerator.CreateMyAlbumZipFile((string[])taskParameters.Value.Value, appPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorLog.GetDefault(null).Log(new Error(ex));
                    }
                }

                Thread.Sleep(50000);
            }
        }
        #endregion
    }
}
